package com.ruoyi.traces.controller;

import java.util.ArrayList;
import java.util.List;

import com.ruoyi.common.core.domain.entity.SysUser;
import com.ruoyi.common.core.domain.model.LoginUser;
import com.ruoyi.common.utils.SecurityUtils;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.traces.domain.TraceUserProduct;
import com.ruoyi.traces.service.ITraceUserProductService;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.common.core.page.TableDataInfo;

/**
 * 用户产品Controller
 * 
 * @author ruoyi
 * @date 2023-02-17
 */
@RestController
@RequestMapping("/trace/user/product")
public class TraceUserProductController extends BaseController
{
    @Autowired
    private ITraceUserProductService traceUserProductService;

    /**
     * 查询用户产品总数
     */
    @GetMapping("/count")
    public int count()
    {
        List<TraceUserProduct> list = new ArrayList<TraceUserProduct>();
        TraceUserProduct traceUserProduct = new TraceUserProduct();
        list = traceUserProductService.selectTraceUserProductList(traceUserProduct);
        return list.size();
    }

    /**
     * 查询用户产品列表
     */
    @GetMapping("/list")
    public TableDataInfo list(TraceUserProduct traceUserProduct)
    {
        startPage();
        List<TraceUserProduct> list = traceUserProductService.selectTraceUserProductList(traceUserProduct);
        return getDataTable(list);
    }
    /**
     * 根据deptId查询用户产品列表
     */
    @GetMapping("/listByDeptId/{deptId}")
    public TableDataInfo list(@PathVariable("deptId") Integer deptId)
    {
        LoginUser loginuser = SecurityUtils.getLoginUser();
        SysUser sysUser = loginuser.getUser();
        startPage();
        List<TraceUserProduct> list = new ArrayList<TraceUserProduct>();
       if(SecurityUtils.isAdmin(sysUser.getUserId())){
           TraceUserProduct traceUserProduct = new TraceUserProduct();
           list = traceUserProductService.selectTraceUserProductList(traceUserProduct);
       }
       else{
           list = traceUserProductService.selectTraceUserProductListByDeptId(deptId);
       }
        return getDataTable(list);
    }
    /**
     * 导出用户产品列表
     */
    @Log(title = "用户产品", businessType = BusinessType.EXPORT)
    @GetMapping("/export")
    public AjaxResult export(TraceUserProduct traceUserProduct)
    {
        List<TraceUserProduct> list = traceUserProductService.selectTraceUserProductList(traceUserProduct);
        ExcelUtil<TraceUserProduct> util = new ExcelUtil<TraceUserProduct>(TraceUserProduct.class);
        return util.exportExcel(list, "product");
    }

    /**
     * 获取用户产品详细信息
     */
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id)
    {
        return AjaxResult.success(traceUserProductService.selectTraceUserProductById(id));
    }

    /**
     * 新增用户产品
     */
    @Log(title = "用户产品", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody TraceUserProduct traceUserProduct)
    {
        return toAjax(traceUserProductService.insertTraceUserProduct(traceUserProduct));
    }

    /**
     * 修改用户产品
     */
    @Log(title = "用户产品", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody TraceUserProduct traceUserProduct)
    {
        return toAjax(traceUserProductService.updateTraceUserProduct(traceUserProduct));
    }

    /**
     * 删除用户产品
     */
    @Log(title = "用户产品", businessType = BusinessType.DELETE)
	@DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable Long[] ids)
    {
        return toAjax(traceUserProductService.deleteTraceUserProductByIds(ids));
    }
}
